﻿#pragma once
using namespace std;
namespace dzj//初阶版本
{
	class string
	{
	public:
		string(const char* str= "")//构造函数
			:_str(new char[strlen(str)+1])
		{
			strcpy(_str, str);
		}
		~string()//析构函数
		{
			delete[] _str;
			_str = nullptr;
		}

		string& operator= (const string & s)//(深拷贝)
		{
			if (this != &s)
			{
				char* tmp = new char[strlen(s._str) + 1];
				delete[] _str;
				_str = tmp;
				strcpy(_str, s._str);
			}
			return*this;
		}

		size_t size()
		{
			return strlen(_str);
		}
		string(const string&s)//拷贝构造函数(深拷贝)
			:_str(new char[strlen(s._str)+1])
		{
			strcpy(_str, s._str);
		}
		char& operator[](size_t pos)//运算符重载
		{
			return _str[pos];
		}
		const char* c_str()
		{
			return _str;
		}
	private:
		char* _str;
	};

	void testString1()
	{
		string s("hello");
		string s1;
		for (size_t i = 0; i < s.size(); i++)
		{
			s[i] += 1;
			cout << s[i] << " ";
		}
	}

	void testString2()
	{
		string s("hello");
		string s1(s);
		cout<<s.c_str()<<endl;
		cout<<s1.c_str()<<endl;

		string s2;
		s2 = s1;
		cout << s2.c_str() << endl;
	}
}

namespace _dzj//进阶版本
{
	class string
	{
	public:

		typedef char* iterator;
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		string(const string& s)
		{
			_str = new char[strlen(s._str) + 1];
			strcpy(_str, s._str);
			_capacity = s._capacity;
			_size = s._size;
		}
		string& operator=(const string& s)
		{
			if (&s != this)
			{
				char* tmp = new char[strlen(s._str) + 1];
				strcpy(tmp, s._str);
				delete[] _str;
				_str = tmp;
				_capacity = _size = strlen(_str);
			}
			return *this;
		}
		size_t size()const
		{
			return _size;
		}

		size_t capacity()
		{
			return _capacity;
		}

		 char* c_str()
		{
			return _str;
		}
		char& operator[](size_t i)
		{
			return _str[i];
		}
		const char& operator[](size_t i)const
		{
			return _str[i];
		}

		string& insert(char ch)
		{
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			_size++;
			return *this;
		}

		string& insert(int pos,char ch)
		{
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
				reserve(newcapacity);
			}
			int end = _size;
			while (end >= pos)
			{
				_str[end + 1] = _str[end];
				end--;
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}

		string& insert(int pos,const char* str)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
				reserve(newcapacity);
			}
			int end = _size;
			while (end >= pos)
			{
				_str[end + len] = _str[end];
				end--;
			}
			strncpy(_str + pos, str,len);
			return *this;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 :_capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		void append(const char*str)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(len + _size);
			}
			strcat(_str, str);
			_size += len;
		}

		string& erase(size_t pos = 0, size_t len = -1)
		{
			if (pos + len > _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				size_t i = pos + len;
				while (i <= _size)
				{
					_str[i - len] = _str[i];
					i++;
				}
				_size -= len;
			}
			return *this;
		}
		size_t find(char c, size_t pos = 0)
		{
			for (size_t i = pos; i < _size; i++)
			{
				if (c == _str[i])
				{
					return i;
				}
			}
			return -1;
		}
		size_t find(const char* s, size_t pos = 0) const
		{
			char* i=strstr(_str, s);
			if (i == nullptr)
			{
				return -1;
			}
			else
			{
				return i - _str;
			}
		}
		string& operator+= (char ch)
		{
			this->push_back(ch);
			return*this;
		}
		bool operator<(const string& s)
		{
			int ret = strcmp(_str, s._str);
			return ret < 0;
		}
		bool operator==(const string& s)
		{
			int ret = strcmp(_str, s._str);
			return ret == 0;
		}
		bool operator<=(const string & s)
		{
			return *this < s || *this == s;
		}
		bool operator>(const string& s)
		{
			return !(*this <= s);
		}
		bool operator>=(const string& s)
		{
			return *this > s || *this == s;
		}
		bool operator!=(const string& s)
		{
			return !(*this==s);
		}
		void resize(size_t n, char c = '\0')
		{
			if (n < _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				if (n > _capacity)
				{
					reserve(n);
				}
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = c;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}
		string& operator+= (const char*str)
		{
			this->append(str);
			return*this;
		}
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str+_size;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};

	

	istream& operator>>(istream& in,  string& s)
	{
		while (1)
		{
			char ch;
			ch = getchar();
			if (ch == ' ' || ch == '\n')
			{
				break;
			}
			s += ch;
		}
		return in;
	}

	ostream& operator<<(ostream&out,const string&s)
	{
		for (size_t i=0; i < s.size(); i++)
		{
			out << s[i];
		}
		return out;
	}
	void testString1()
	{
		string s1;
		string s2("hello");
		string s3(s2);
		s1 = s3;
		string s4;
		string::iterator  it = s1.begin();
		while (it != s1.end())
		{
			cout << *it;
			it++;
		}
	}

	void testString2()
	{
		string s1("hello");
		s1 += ' ';
		s1 += "world";
		s1.insert(0,'x');
		s1.insert(0,"kkk");
		cout << s1;
	}


	void testString3()
	{
		string s1("hello");
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
		s1.resize(10,'x');
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
		s1.reserve(50);
		s1.resize(10, 'x');
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
		cout << s1;
	}


	void testString4()
	{
		string s("abcdeefjdsak");
		size_t i = s.find('d', 2);
		cout << i << endl;
		i = s.find("eef", 2);
		cout << i << endl;
		i = s.find("ddd", 2);
		cout << i << endl;
	}

	void testString5()
	{
		string s;
		cin >> s;
		string s1;
		cin >> s1;
		cout << (s>s1) << endl;
	}
}


namespace dzj_copy//终极版写法,主要是有关深拷贝的新写法
{
	class string
	{
	public:
		string(const char* str="")
			:_str(new char[strlen(str) + 1])
		{
			strcpy(_str, str);
		}

		string(const string&s)//深拷贝
			:_str(nullptr)
		{
			string tmp(s._str);
			swap(_str, tmp._str);
		}

		string& operator=(const string& s) // 深拷贝
		{
			if (&s != this) // 自我赋值检查
			{
				string temp(s); // 使用临时对象进行拷贝构造
				swap(*this, temp); // 安全地交换
			}
			return *this;
		}


		~string()
		{
			delete[] _str;
			_str = nullptr;
		}
	private:
		char* _str;
	};


	void testString1()
	{
		string s1("hello");
		string s2;
		string s3(s1);
		s2 = s3;
	}
}